﻿#pragma once
#include <Windows.h>
#include <vector>
#include <string>

using std::string;

struct Capturer
{
    int x;
    int y;
    int width;
    int height;

    std::string name;
    void* hwnd = nullptr;

    void* buffer = nullptr;

    bool cursor = false;

    // 截图类型
    int type = 0;
};

class DibCaptureHelper
{
public:
    DibCaptureHelper();
    virtual ~DibCaptureHelper();

private:
    bool Init(HWND hwnd);
    void Cleanup();
    bool RefreshWindow();

public:
    bool ChangeWindowHandle(const Capturer& capturer);
    bool Capture();

    const RECT& GetWindowRect() const { return windowRect_; }
    const RECT& GetClientRect() const { return clientRect_; }
    int GetBitmapDataSize() const { return bmpDataSize_; }
    HBITMAP GetBitmap() const { return bitmap_; }
    void* GetBitmapAddress() const { return _capturer.buffer; }
    const HDC& GetHdc() const { return memDc_; }

    static void EnumCapturers(std::vector<Capturer>* capturers);

private:
    HWND hwnd_;
    HDC scrDc_;
    HDC memDc_;
    HBITMAP bitmap_;
    HBITMAP oldBitmap_;

    RECT windowRect_;
    RECT clientRect_;
    int bmpDataSize_;

    Capturer _capturer;

    static BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor,
        HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData);

    static BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam);
};